package leetcode_2101_2200;

import java.util.Arrays;

public class LeeCode_2174 {
    public static void main(String[] args) {
        System.out.println(minimumRemoval(new int[]{4, 1, 6, 5}));
    }
    private static long minimumRemoval(int[] beans) {
        int n = beans.length;
        Arrays.sort(beans);
        long[] sums = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            sums[i] = sums[i - 1] + beans[i - 1];
        }
        long ans = Long.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            ans = Math.min(ans, sums[i] + (sums[n] - sums[i + 1] - (long) beans[i] * (n - 1 - i)));
        }
        return ans;
    }
}
